import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler, LabelEncoder
from sklearn.model_selection import train_test_split
import keras
from keras.layers import Dense
from keras.utils.np_utils import to_categorical


plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

dateset = pd.read_csv('./data.csv')

le = LabelEncoder()
dateset['target'] = le.fit_transform(dateset['diagnosis'])
dateset.drop("Unnamed: 32", axis=1, inplace=True)
dateset.drop("id", axis=1, inplace=True)
dateset.drop("diagnosis", axis=1, inplace=True)
print(dateset)

Y = dateset['target']
X = dateset.iloc[:, :-1]

# 划分数据集
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)

# 将数据集标签转化为one-hot向量格式
y_train_one = to_categorical(y_train, 2)
y_test_one = to_categorical(y_test, 2)

# 进行归一化操作
sc = MinMaxScaler(feature_range=(0, 1))
x_train = sc.fit_transform(x_train)
x_test = sc.fit_transform(x_test)

model = keras.Sequential()
model.add(Dense(40, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(2, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='SGD', metrics=['accuracy'])

history = model.fit(x_train, y_train_one, epochs=95, batch_size=16, verbose=2, validation_data=(x_test, y_test_one))
model.save('model.h5')

plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='val')
plt.title('全连接神经网络loss值图')
plt.legend()
plt.show()

plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'], label='val')
plt.title('全连接神经网络accuracy值图')
plt.legend()
plt.show()
